package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class MONIYOUXIGEAttr {
    @Resource
    GameAttributeMapper gameAttributeMapper;
    @Resource
    private RedisCache redisCache;


    private static String SERVICE_NAME = "KJS";
    private static String getCookie(SalePartner salePartner){

//        String s = new Date().getTime() / 1000 + "";

        String cookie = "account=" + salePartner.getSaleKey() +"; _ga=GA1.1.2133416559.1726621808; acw_tc=ac11000117266272807932614e00e00608a859cf72b9d0f1ed3cb79246450c; _ga_TJJQCLLECR=GS1.1.1726627284.2.0.1726627284.0.0.0";
        return cookie;
    }


    public static void main(String[] args) {
        String url = "http://www.youxige.com/tools/daishou/getGameServer.ashx?cid=40";
        String response = HttpRequest.post(url).header("Cookie", "Hm_lvt_ddd1dd7d74725efb31c09c4d77343558=1733821782; HMACCOUNT=3309EA28EF2272B8; Hm_lvt_8ba4717310dcf202fa0c1fb02a53a570=1735096362; ASP.NET_SessionId=y3rmbbeji5v12zqip20ejeqj; dt_cookie_user_name_remember=JFcms=18093522409; dt_cookie_user_pwd_remember=JFcms=BEB0B745AC0A078B9AB8FEE75985AE0B; Hm_lpvt_ddd1dd7d74725efb31c09c4d77343558=1735097167; Hm_lpvt_8ba4717310dcf202fa0c1fb02a53a570=1735097167").execute().body();
        JSONArray jsonArray = JSONArray.parseArray(response);
        for(Object o : jsonArray) {
            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
            System.out.println(jsonObject1.getString("id")+"="+jsonObject1.getString("title"));
        }
    }

    public List<GamesRes> getGames(SalePartner salePartner) {
        String string = "18-地下城与勇士,20-穿越火线,21-英雄联盟,75-无畏契约,58-永劫无间,31-逆战,30-QQ飞车,49-三国杀,48-CSOL,53-QQ炫舞,76-瓦洛兰特,64-绝地求生,78-命运方舟,79-黑神话悟空,81-魔兽世界正式服,82-魔兽世界怀旧服,83-炉石传说,69-DNF辟邪玉,70-DNF手游,22-王者荣耀,40-枪战王者,33-和平精英,44-原神,41-QQ飞车手游,60-光-遇,32-火影忍者,47-球球大作战,80-永劫无间手游,62-英雄联盟手游,65-金铲铲之战,72-蛋仔派对,71-奥比岛手游,74-崩坏星穹铁道,59-使命召唤,77-逆水寒,61-第五人格,84-街头篮球,85-三角洲行动,86-自由幻想";
        String[] split = string.split(",");
        List<GamesRes> gamesResList = new ArrayList<>();
        for (String s : split) {
            GamesRes gamesRes = new GamesRes();
            String id = s.split("-")[0];
            String name = s.split("-")[1];
            gamesRes.setId(id);
            gamesRes.setGameName(name);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String gameId,SalePartner salePartner) {
        List<InterServerRes> interServerRes = new ArrayList<>();
        String url = "http://www.youxige.com/tools/daishou/getGameServer.ashx?cid="+gameId;
        String response = HttpRequest.post(url).header("Cookie", salePartner.getTwoOther()).execute().body();
        JSONArray jsonArray = JSONArray.parseArray(response);
        for(Object o : jsonArray) {
            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
            InterServerRes interServer = new InterServerRes();
            interServer.setId(jsonObject1.getString("id"));
            interServer.setName(jsonObject1.getString("title"));
            interServer.setUuid(UUIDGenerator.generateUuid6());
            if(gameId.equals("21")){
                List<InterServerRes> resList = new ArrayList<>();
                String url2 = "http://www.youxige.com/tools/daishou/getGameServer.ashx?qid="+jsonObject1.getString("id")+"&cid=21";
                String response2 = HttpRequest.post(url2).header("Cookie", salePartner.getTwoOther()).execute().body();
                JSONArray jsonArray2 = JSONArray.parseArray(response2);
                for(Object p : jsonArray2) {
                    JSONObject jsonObject2 = JSONObject.parseObject(p.toString());
                    InterServerRes interServer2 = new InterServerRes();
                    interServer2.setId(jsonObject2.getString("id"));
                    interServer2.setName(jsonObject2.getString("title"));
                    resList.add(interServer2);
                }
                interServer.setInterServerRes(resList);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        String url = "http://www.youxige.com/release.aspx?cid="+gameId;
        String response = HttpRequest.get(url).header("Cookie", salePartner.getTwoOther()).execute().body();
        Document document = Jsoup.parse(response);
        Elements elements = document.getElementsByClass("ct");
        for(Element element : elements){
            Elements dl = element.select("dl");
            for(Element dls : dl){
                if (Objects.equals("1", type)) {
                    if (dls.attr("control_type").equals("multi-radio")) {
                        InterAttrRes interAttrRes = new InterAttrRes();
                        if(StringUtils.isNotEmpty(dls.attr("name"))) {
                            interAttrRes.setId(dls.attr("name"));
                            interAttrRes.setName(dls.select("span").text());
                            interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                            String li = dls.select("li").text();
                            String[] inputs = li.split(" ");
                            for (String s : inputs) {
                                InterAttrRes interAttrRes1 = new InterAttrRes();
                                interAttrRes1.setName(s);
                                interAttrRes1.setId(s);
                                interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                                interAttrResList1.add(interAttrRes1);
                            }
                            interAttrRes.setInterServerRes(interAttrResList1);
                            interAttrResList.add(interAttrRes);
                        }else {
                            interAttrRes.setId(dls.attr("class").split(" ")[1]);
                            interAttrRes.setName(dls.select("span").text());
                            interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                            Elements li = dls.select("li");
                            for(Element lis : li){
                                InterAttrRes interAttrRes1 = new InterAttrRes();
                                interAttrRes1.setId(lis.attr("value"));
                                interAttrRes1.setName(lis.text());
                                interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                                interAttrResList1.add(interAttrRes1);
                            }
                            interAttrRes.setInterServerRes(interAttrResList1);
                            interAttrResList.add(interAttrRes);
                        }
                    }
                } else if (Objects.equals("2", type)) {
                    if (dls.attr("control_type").equals("multi-checkbox")) {
                        InterAttrRes interAttrRes = new InterAttrRes();
                        interAttrRes.setId(dls.attr("name"));
                        interAttrRes.setName(dls.select("span").text());
                        interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                        List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                        String li = dls.select("li").text();
                        String[] inputs = li.split(" ");
                        for (String input : inputs) {
                            InterAttrRes interAttrRes1 = new InterAttrRes();
                            interAttrRes1.setId(input);
                            interAttrRes1.setName(input);
                            interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                            interAttrResList1.add(interAttrRes1);
                        }
                        interAttrRes.setInterServerRes(interAttrResList1);
                        interAttrResList.add(interAttrRes);
                    }
                } else if (Objects.equals("3", type)) {
                    if (dls.attr("control_type").equals("number")||dls.attr("control_type").equals("single-text")) {
                        InterAttrRes interAttrRes = new InterAttrRes();
                        interAttrRes.setId(dls.attr("name"));
                        interAttrRes.setName(dls.select("span").text());
                        interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                        interAttrResList.add(interAttrRes);
                    }
                }
            }
        }
        return interAttrResList;
    }

}